﻿using Aker.DevTeam;
using DevTeam;
using Microsoft.VisualBasic.ApplicationServices;
using System;
using System.Threading;
using System.Windows.Forms;

namespace Aker
{
    public class SingleInstanceController : WindowsFormsApplicationBase
    {
        public SingleInstanceController()
        {
            IsSingleInstance = true;

            StartupNextInstance += this_StartupNextInstance;
        }

        public void this_StartupNextInstance(object sender, StartupNextInstanceEventArgs e)
        {
            e.BringToForeground = true;
        }

        protected override void OnCreateMainForm()
        {
            using (Login login = new Login())
                if (login.ShowDialog() == DialogResult.OK)
                {
                    AppSession.MainForm = new MainForm();
                    AppSession.MainForm.ShowInTaskbar = false;
                    AppSession.MainForm.WindowState = FormWindowState.Minimized;

                    MainForm = AppSession.MainForm;
                }
                else
                {
                    Form dummy = new Form();
                    MainForm = dummy;
                    dummy.Close();
                }
        }
    }

    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            InitLookAndFeel();

            Application.ThreadException += new ThreadExceptionEventHandler(new ThreadExceptionHandler().ApplicationThreadException);

            // Add the event handler for handling non-UI thread exceptions to the event. 
            AppDomain.CurrentDomain.UnhandledException += new System.UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

            // Set the unhandled exception mode to force all Windows Forms errors to go through our handler.
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

            if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
                AppSession.Version = System.Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString();

            SingleInstanceController controller = new SingleInstanceController();

            try
            {
                controller.Run(new string[] { });
            }
            catch (Exception ex)
            {

            }
        }

        private static void InitLookAndFeel()
        {
            Application.EnableVisualStyles();
            DevExpress.Skins.SkinManager.EnableFormSkins(); // IfNotVista();

            //Register the skins with the designer
            DevExpress.Skins.SkinManager.Default.RegisterAssembly(typeof(DevExpress.UserSkins.OfficeSkins).Assembly);
            Application.SetCompatibleTextRenderingDefault(false);
        }

        static void CurrentDomain_UnhandledException(object sender, System.UnhandledExceptionEventArgs e)
        {
            Exception ex = (Exception)e.ExceptionObject;
            LogManager.LogToDB(ex);
            // XtraMessageBox.Show(e.ToString(), "Exception");
        }
    }

    public class ThreadExceptionHandler
    {
        public void ApplicationThreadException(object sender, ThreadExceptionEventArgs e)
        {
            Exception ex = e.Exception;
            Type type = ex.GetType();
            if (type == typeof(ApplicationException))
                LogManager.Notify(ex);
            else //else if (! (e.Exception.ToString().Contains("0x80131904") && e.Exception.ToString().Contains("RepositoryManager.RefreshData")))
                LogManager.LogToDB(ex);
            //XtraMessageBox.Show(e.Exception.ToString(), "Exception");
        }
    }
}